import {defineStore} from "pinia";
import {reactive} from "vue";
import myAxios from "../plugins/myAxios.ts";
import {Message} from "@arco-design/web-vue";

export const allDataStore = defineStore('allData', () => {
    const showData = reactive({
        medicineNum: -1,
        postNum: 0,
        prescriptionNum: 0,
        userNum: 0
    })
    const medicinesRes = reactive({
        countId: null,
        current: 1,
        maxLimit: null,
        optimizeCountSql: true,
        orders: [],
        pages: 1,
        records: [],
        searchCount: true,
        size: 14,
        total: 0
    })
    const prescriptionRes = reactive({
        countId: null,
        current: 1,
        maxLimit: null,
        optimizeCountSql: true,
        orders: [],
        pages: 1,
        records: [],
        searchCount: true,
        size: 14,
        total: 0
    })
    const postRes = reactive({
        countId: null,
        current: 1,
        maxLimit: null,
        optimizeCountSql: true,
        orders: [],
        pages: 1,
        records: [],
        searchCount: true,
        size: 14,
        total: 0
    })
    const userRes = reactive({
        countId: null,
        current: 1,
        maxLimit: null,
        optimizeCountSql: true,
        orders: [],
        pages: 1,
        records: [],
        searchCount: true,
        size: 14,
        total: 0
    })
    const getAll = () => {
        if (showData.medicineNum == -1) {
            myAxios.get("/admin/get/all").then(res => {
                showData.medicineNum = parseInt(res.data.medicineNum);
                showData.postNum = parseInt(res.data.postNum);
                showData.prescriptionNum = parseInt(res.data.prescriptionNum);
                showData.userNum = parseInt(res.data.userNum);
            })
        } else {
            Message.success('已存入展示数据')
        }

    }
    const getMedicines = async (current: any, pageSize: any) => {
        async function getData() {
            const res = await myAxios.post('/medicine/info', {
                current: current,
                pageSize: pageSize
            })
            medicinesRes.current = parseInt(res.data.current)
            medicinesRes.size = parseInt(res.data.size)
            medicinesRes.total = parseInt(res.data.total)
            medicinesRes.records = res.data.records
        }
        await getData();
        // if (medicinesRes.records.length === 0) {
        //     await getData();
        // } else if(current != medicinesRes.current) {
        //     await getData();
        // } else {
        //     Message.success('已存入药材数据')
        // }
    }

    const getPrescription = async (current: any, pageSize: any) => {
        async function getData() {
            const res = await myAxios.post('/medicine/list/page', {
                current: current,
                pageSize: pageSize
            })
            prescriptionRes.current = parseInt(res.data.current)
            prescriptionRes.size = parseInt(res.data.size)
            prescriptionRes.total = parseInt(res.data.total)
            prescriptionRes.records = res.data.records
        }
        await getData();
        // if (prescriptionRes.records.length === 0) {
        //     await getData();
        // } else if(current != prescriptionRes.current) {
        //     await getData();
        // } else {
        //     Message.success('已存入处方数据')
        // }
    }

    const getPosts = async (current: any, pageSize: any) => {
        async function getData() {
            const res = await myAxios.post('/post/list/page/vo', {
                current: current,
                pageSize: pageSize
            })
            postRes.current = parseInt(res.data.current)
            postRes.size = parseInt(res.data.size)
            postRes.total = parseInt(res.data.total)
            postRes.records = res.data.records
        }
        await getData();
        // if (postRes.records.length === 0) {
        //     await getData();
        // } else if(current != postRes.current) {
        //     await getData();
        // } else {
        //     Message.success('已存入帖子数据')
        // }
    }

    const getUsers = async (current: any, pageSize: any) => {
        async function getData() {
            const res = await myAxios.post('/user/list/page', {
                current: current,
                pageSize: pageSize
            })
            userRes.current = parseInt(res.data.current)
            userRes.size = parseInt(res.data.size)
            userRes.total = parseInt(res.data.total)
            userRes.records = res.data.records
        }
        await getData();
        // if (userRes.records.length === 0) {
        //     await getData();
        // } else if(current != userRes.current) {
        //     await getData();
        // } else {
        //     Message.success('已存入用户数据')
        // }
    }
    return {showData, medicinesRes, prescriptionRes, postRes, userRes, getAll, getMedicines, getPrescription, getPosts, getUsers}
})
